﻿using System;
using System.Text;

namespace BizElements.Core.ProviderComponents
{
    /// <summary>Generates INSERT statements and, if required by implementation, SELECT statements that fetch next ID/sequence.</summary>
    public interface IInserter
    {
        /// <summary>
        /// Renders insert statement.
        /// </summary>
        /// <param name="insert">INSERT statement that is being rendered.</param>
        /// <param name="nextSequence">Parameter that contains the next ID/sequence value. <b>null</b> if DBMS doesn't use sequences or table doesn't contains an auto-id field.</param>
        /// <param name="dbms">Target DBMS.</param>
        /// <param name="output">StringBuilder to which the SQL code is appended.</param>
        /// <param name="parameters">SQL parameter collection to which the object's and its children's parameters are added. After the rendering is done the collection contains all parameters with unique names.</param>
        /// <returns>Parameter that contains the ID of the inserted row. <b>null</b> if auto-generated value is not returned via output parameter.</returns>
        DbParameter RenderInsert(InsertStatement insert, DbParameter nextSequence, DbmsType dbms, StringBuilder output, DbParameterCollection parameters);

        /// <summary>Executes the INSERT command. Automatically generates the code that retrieves the new identity for 
        /// the supported databases. DBMS specific code depends on the DBMS property of the used ConnectionProvider.</summary>
        /// <param name="insert">INSERT statement to execute.</param>
        /// <param name="dbms">Target DBMS.</param>
        /// <param name="conn">Connection-transaction context to use.</param>
        /// <param name="lastExecutedCommandInfo">Output parameter: statistic for executed command.</param>
        /// <returns>Automatically generated ID for inserted row, or <b>null</b> if ID is not automatically generated.</returns>
        object Execute(InsertStatement insert, DbmsType dbms, IConnectionProvider conn, out CommandExecutionStatistics lastExecutedCommandInfo);
    }
}